<?php

namespace App\Http\Controllers\Department;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Divide\Department;
use App\Models\Divide\DepartmentSplitRatio;
use App\Models\Divide\PdUserRelationship;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DepartmentController extends Controller
{
    //
    use ResponseData;
    public function getDepartmentList(Department $model, Request $request){
        $model = $model->select('category_id','department_id','department_name','ratio','code','description');
        if($request->filled('category_id')){
            $model = $model->where('category_id',$request->category_id);
        }
        $model = $model->get();
        return $this->success($model);

    }
    public function save(Department $model, Request $request){
        if(!$request->filled('category_id')){
            return $this->failed('请传递品类id',201);
        }
        if($request->filled('department_id')){
            $model = $model->query()->find($request->department_id);
            if($model){
                $model->department_name = $request->department_name;
                $model->code = $request->code;
                $model->description = $request->description;
                $model->ratio = $request->ratio;
                $model->save();
                return $this->success($model);
            }
        }else{
            $data = $request->except('department_id','id');
            $res = $model->query()->create($data);
           return $this->success($res);
        }

    }
    // 删除部门
    public function destory(Department $model, Request $request){
        if(!$request->filled('category_id')){
            return $this->failed('请传递品类id',201);
        }
        if(!$request->filled('department_id')){
            return $this->failed('请传递部门ID',201);
        }
        DB::transaction(function () use($model,$request){
            $model = $model->query()->find($request->department_id);
            $res = $model->delete();
            if($res){
                $res = DepartmentSplitRatio::query()->where('category_id',$request->category_id)
                    ->where('type',1)
                    ->where('department_id',$request->department_id)
                    ->delete();
            }
        });


        return $this->success('删除数据成功');

    }
    public function getEmployee(DepartmentSplitRatio $model, Request $request){
        if(!$request->filled('category_id')){
            return $this->failed('请上传分类id',201);
        }
        $model = $model->query()
            ->select('id','name','avatar','category_id','department_id','user_id','ratio')
            ->where('category_id',$request->category_id);
        if($request->filled('department_id')){
            $model = $model->where('department_id',$request->department_id);
        }
        $data = $model->get();
        return $this->success($data);

    }
    // 获取部门用户比例
    public function getDepartmentUserRatio(DepartmentSplitRatio $model, Request $request){
        $data = $model->query()
            ->where('category_id',$request->category_id)
            ->where('department_id',$request->department_id)
            ->sum('ratio');
        return $this->success($data);
    }
    public function deleteEmployee(DepartmentSplitRatio $model, Request $request){
        $data = $model->query()->find($request->id);
        if($data){
            $data->delete();
        }
        return $this->success('删除成功');
    }
    public function saveEmployee(DepartmentSplitRatio $model, Request $request){
        $type = $request->input('type',1);
        if(is_array($request->user)){
            $users = $request->user;
        }else{
            $users = json_decode($request->user,true);
        }
        foreach ($users as $item){
            if(isset($item['id'])){ // 存在则更新，不存在则创建
                $user = DepartmentSplitRatio::query()->find($item['id']);
                if($user){
                    $user->ratio = $item['ratio'];
                    $user->save();
                }
                continue;
            }
            $data['user_id'] = $item['nid'];
            $data['name'] = $item['name'];
            $data['avatar'] = $item['avatar'];
            $data['category_id'] = $request->category_id;
            $data['department_id'] = $request->department_id;
            $data['ratio'] = $item['ratio'];
            $model->query()->create($data);
        }

        return $this->success('保存成功');
    }
    // 获取选手列表
    public function getUsers(PdUserRelationship $model,Request $request){
        $model = $model->query()
            ->select('nid','name','avatar')
            ->where('role',3);
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        if($request->filled('user_id')){
            $model = $model->where('nid',$request->user_id);
        }
        $data= $model->paginate(20);
        return $this->success($data);
    }
}
